<template>
  <a-card>
    <div class="ContactPerson">
      <div class="searchNav" style="display: flex; justify-content: space-between;">
        <a-button class="button" icon="el-icon-plus" type="primary" style="margin-left: 0"
          @click="handleAdd">新增</a-button>
        <a-button class="button1" type="text" @click="EditEmail">配置邮箱</a-button>
        <div class="searchParams" style="display: flex;padding-bottom: 10px;">
          <div>
            <radioGroup @radioChange="radioChange" @complete="radioComplete" />
          </div>
          <a-input style="margin: 0 10px;" class="input" v-model="queryParam.keyword" placeholder="请输入承运商名称/编码" clearable
            @keydown.enter.native="getDataList"></a-input>
          <a-button class="button" @click="getDataList" style="background-color: #1890ff; color: #fff;">查询</a-button>
        </div>
      </div>
      <div>
        <el-table :data="tableData" style="width: 100%" :max-height="height" v-loading="loading">
          <!-- <el-table-column prop="CompanyName" label="所属公司" width="200">
        </el-table-column> -->
          <el-table-column prop="CustomerName" label="客户"> </el-table-column>
          <el-table-column prop="CreateTime" label="日期">
            <template slot-scope="scope">
              {{ scope.row.CreateTime ? scope.row.CreateTime.substring(0, 10) : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="State" label="状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.State == 0">待确认</el-tag>
              <el-tag v-if="scope.row.State == 1" type="success">已确认</el-tag>
              <el-tag v-if="scope.row.State == 2" type="danger">已拒绝</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="260" align="center">
            <template slot-scope="scope">
              <a-button style="background-color: #1890ff;color: #fff;" type="text"
                @click="handleView(scope.row)">查看</a-button> &nbsp;
              <a-button style="background-color: #1890ff;color: #fff;" type="text" @click="handleEdit(scope.row)"
                v-if="scope.row.State == 0">编辑</a-button>&nbsp;
              <a-button style="background-color: #1890ff;color: #fff;" type="text"
                @click="handleSend(scope.row)">发送</a-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination layout="total,prev, pager, next,sizes" :total="pagination.total" :current-page="pagination.current"
          :page-size="pagination.pageSize" :page-sizes="pagination.pageSizeOptions"
          style="text-align: right; margin-top: 8px; padding: 0" @current-change="currentChange"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
      <el-dialog title="客户邮箱" :visible.sync="dialogVisible" width="30%">
        <a-input v-model="Email" placeholder="请输入客户邮箱"></a-input>
        <span slot="footer" class="dialog-footer">
          <a-button @click="dialogVisible = false">取 消</a-button>
          <a-button type="primary" :loading="dialogLoading" @click="handleClose">确 定</a-button>
        </span>
      </el-dialog>
      <el-dialog title="发送对账函" :visible.sync="sendVisible" width="30%">
        <div class="send">
          <div class="sendItem">
            <img src="@/assets/weixin.png" alt="微信" @click="sendMethods = 'wx'" />
            <el-radio v-model="sendMethods" label="wx"> 微信 </el-radio>
          </div>
          <div class="sendItem">
            <img src="@/assets/youxiang.png" alt="邮箱" @click="sendMethods = 'email'" />
            <el-radio v-model="sendMethods" label="email"> 邮箱 </el-radio>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <a-button @click="sendVisible = false">取 消</a-button>
          <a-button type="primary" :loading="dialogLoading" @click="handleSendClose">确 定</a-button>
        </span>
      </el-dialog>
      <el-dialog title="微信扫一扫分享" :visible.sync="wxVisible" width="30%">
        <div style="text-align: center">
          <vue-qr :margin="0" :text="wxCode" :size="160"></vue-qr>
        </div>
      </el-dialog>
      <Email ref="Email" />
      <a-drawer v-if="jumpId ? true : false" title="送货单" width="850px" placement="right" :closable="false"
        :visible="jumpId ? true : false" @close="() => {
          this.jumpId = null;
        }">
        <!-- <SendSMS  :jump="jumpId"></SendSMS> -->
        <iframe style="width: 100%;height:88vh;border: 0;"
          :src="`https://hzq.yoojet.com/#/SendSMSByThirdParty?prop=${jumpId}`"></iframe>
      </a-drawer>
      <EditForm ref="editForm" :parentObj="{ ...this }" />
    </div>
  </a-card>
</template>

<script>
import VueQr from "vue-qr";
import radioGroup from "@/components/components/CompanyRadioGroup.vue";
import EditForm from "./ReconciliationForm.vue";
import Email from "./Email.vue";
export default {
  props: {},
  components: {
    EditForm,
    radioGroup,
    Email,
    VueQr,
  },
  mounted() { },
  created() { },
  computed: {
    height() {
      return document.documentElement.clientHeight * 0.75;
    },
  },
  data() {
    return {
      wxCode: "",
      wxVisible: false,
      sendMethods: "wx",
      sendVisible: false,
      dialogVisible: false,
      pagination: {
        current: 1,
        pageSize: 25,
        total: 0,
        pageSizeOptions: [25, 50, 100],
      },
      sorter: {
        field: "Id",
        order: "desc",
      },
      tableData: [],
      fileList: [],
      queryParam: { SupplieId: "全部" },
      loading: false,
      dialogVisible: false,
      Email: "",
      row: {},
      dialogLoading: false,
      jumpId: null,
    };
  },
  methods: {
    handleSend(row) {
      this.sendVisible = true;
      this.row = row;
    },
    handleSendClose() {
      if (this.sendMethods == "wx") {
        this.wxVisible = true;
        this.wxCode =
          location.href.split("#")[0] +
          "#/ReconciliationMobile?Id=" +
          this.row.Id;
        console.log(this.wxCode);
      } else if (this.sendMethods == "email") {
        // 发送邮箱
        this.dialogVisible = true;
      }
      this.sendVisible = false;
    },
    // 配置邮箱
    EditEmail() {
      if (this.queryParam.SupplieId.split(",").length > 1)
        return this.$message.error("请选择公司");

      this.$refs.Email.openDialog(this.queryParam.SupplieId);
    },
    // 邮箱确认
    handleClose() {
      if (!this.Email) return this.$message.error("请输入邮箱");
      this.dialogLoading = true;
      this.$httpp
        .post("/Base/Base_Reconciliation/SendMail", {
          Id: this.row.Id,
          CompanyId: this.row.Company,
          RecipientEmail: this.Email,
          CcusName: this.row.CustomerName,
          CodeDiv: this.row.LetterCode
            ? `<div>对账函密码:${this.row.LetterCode}</div>`
            : "<div></div>",
          Url: "https://hzq.yoojet.com/#/SendSMSMobileCopy?prop=" + this.row.Id,
        })
        .then((res) => {
          this.dialogLoading = false;
          if (res.Success) {
            this.dialogVisible = false;
            this.$message.success("发送成功");
          } else {
            this.$message.error(res.Msg);
          }
        });
    },
    radioComplete(e) {
      if (e) {
        this.queryParam.SupplieId = e;
        this.getDataList();
      }
    },
    radioChange(e) {
      if (e == "more") return;
      this.queryParam.SupplieId = e;
      this.getDataList();
    },
    handleAdd() {
      if (this.queryParam.SupplieId.split(",").length > 1) {
        return this.$message.error("请选择公司");
      } else {
        this.$httpp
          .post(
            "/Base/Base_Reconciliation/GetCompanyTemp?CompanyId=" +
            this.queryParam.SupplieId,
            {}
          )
          .then((res) => {
            console.log(res);
            if (res.Success) {
              this.$refs.editForm.openForm(
                "新增对账函",
                res.Data.Content,
                this.queryParam.SupplieId
              );
            } else {
              this.$message.error(res.Msg);
            }
          });
      }
    },
    handleEdit(row) {
      this.$refs.editForm.openForm("编辑对账函", row.Id, row.Company);
    },
    handleView(row) {
      let token = JSON.parse(localStorage.getItem('ReceiptSign_token')).token;
      this.jumpId = row.Id + '&' + token;
    },
    currentChange(page) {
      this.pagination.current = page;
      this.getDataList();
    },
    handleSizeChange(lint) {
      this.pagination.pageSize = lint;
      this.getDataList();
    },
    getDataList() {
      this.loading = true;
      this.$httpp
        .post("/Base/Base_Reconciliation/GetRectionList", {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || "Id",
          SortType: this.sorter.order,
          Search: this.queryParam,
        })
        .then((resJson) => {
          this.loading = false;
          if (resJson.Success) {
            this.tableData = resJson.Data;
            if (this.tableData) {
              const pagination = { ...this.pagination };
              pagination.total = resJson.Total;
              this.pagination = pagination;
            }
          }
        });
    },
  },
  watch: {},
};
</script>

<style lang="less" scoped>
.send {
  display: flex;
  justify-content: space-around;

  .sendItem {
    display: flex;
    flex-direction: column;
    align-items: center;

    img {
      display: block;
      width: 50px;
      margin-bottom: 10px;
      cursor: pointer;
    }
  }
}

.del {
  color: red;
}

.button {
  background-color: #1890ff;
  border: #1890ff;
}

.button:hover {
  background-color: #1890ff;
}

.button1 {
  color: #1890ff;
}
</style>
